import React, { useEffect, useContext } from 'react'
import ajax from './data'
import context from './contextCreator'

export default function User() {

  const { state, dispatch } = useContext(context)

  useEffect(() => {
    // ajax('/user').then((user) => {
    //   dispatch({
    //     type: 'user',
    //     user
    //   })
    // })
    ;(async () => {
      let user = await ajax('/user')
      dispatch({
        type: 'user',
        user
      })
    })()
  }, [dispatch])

  return (
    <div>User: {state.user && state.user.name}</div>
  )
}
